#################################################
############2020 DATASET########################

######################################
### H1: Immigration as Positional Cue
######################################
# Hypothesis 1: Immigration as a Positional Cue
# Baseline logistic regression
model_h1 <- glm(vote_choice_binary ~ immigration_ideology, 
                data = anes_2020, 
                family = binomial(link = "logit"))

summary(model_h1)

######################################
### H2: Immigration as Hybrid Valence
######################################

#Hypothesis 2: Immigration as a Hybrid Valence Issue
# Logistic regression with immigration + relative competence & honesty
model_h2 <- glm(vote_choice_binary ~ immigration_ideology + rel_comp + rel_honest,
                data = anes_2020,
                family = binomial(link = "logit"))

summary(model_h2)

######################################
### H3: Issue Absorption
######################################

#Hypothesis 3: Immigration × Partisanship Interaction
# Clean partisanship variable: Dem = 1, Ind = 2, Rep = 3 (simplify 7-point scale if needed)
anes_2020 <- anes_2020 %>%
  mutate(pid_simple = case_when(
    pid %in% c(1,2,3) ~ "Democrat",
    pid == 4 ~ "Independent",
    pid %in% c(5,6,7) ~ "Republican",
    TRUE ~ NA_character_
  )) %>%
  mutate(pid_simple = factor(pid_simple, levels = c("Independent", "Democrat", "Republican")))

# Logistic regression with interaction
model_h3 <- glm(vote_choice_binary ~ immigration_ideology + rel_comp + rel_honest + immigration_ideology * pid,
                data = anes_2020,
                family = binomial(link = "logit"))

summary(model_h3)

################ Marginal effects / predicted probabilities ###################
##################### H1, H2, H3 -----2020#####################################
###############################################################################
install.packages("margins")
library(margins)
margins_h1 <- margins(model_h1)
summary(margins_h1)

margins_h2 <- margins(model_h2)
summary(margins_h2)

margins_h3 <- margins(model_h3)
summary(margins_h3)

######################################
### Plot interaction
######################################
library(ggplot2)
library(effects)
plot(allEffects(model_h1), main = "Immigration Attitudes ~ Vote choice")
plot(allEffects(model_h2), main = "Immigration Attitudes ~ Valence")
plot(allEffects(model_h3), main = "Immigration Attitudes × Partisanship")


######################################
### Compute AME
######################################
library(margins)

marg_2020 <- margins(model_h3, 
                     variables = c("immigration_ideology", "rel_comp", "rel_honest", "pid"))
summary(marg_2020)

#COMPUTE FIRST DIFFERENCE--JUST FOR 2020
library(ggeffects)

# Immigration ideology
pred_immig_2020 <- ggpredict(model_h3, 
                             terms = c("immigration_ideology [1,5]"))

# Competence
pred_pid_2020 <- ggpredict(model_h3, 
                           terms = c("pid [1,7]"))

# Honesty
pred_comp_2020 <- ggpredict(model_h3, 
                            terms = c("rel_comp [1,5]"))

# Partisanship
pred_honest_2020 <- ggpredict(model_h3, 
                              terms = c("rel_honest [1,5]"))

# View differences
pred_immig_2020
pred_comp_2020
pred_honest_2020
pred_pid_2020


##############################################################
### CREATE FOUR-PANEL PLOT 2020 (for Biden | Trump is the Ref)
##############################################################

#only plots for 2020
library(ggplot2)
library(cowplot)

p1 <- plot(pred_immig_2020) + 
  labs(title = "Immigration Ideology", x = "Immigration (liberal → restrictive)", y = "Pr(Vote Biden)")

p2 <- plot(pred_comp_2020) + 
  labs(title = "Relative Competence (Biden – Trump)", x = "Competence Advantage", y = "Pr(Vote Biden)")

p3 <- plot(pred_honest_2020) + 
  labs(title = "Relative Honesty (Biden – Trump)", x = "Honesty Advantage", y = "Pr(Vote Biden)")

p4 <- plot(pred_pid_2020) + 
  labs(title = "Partisanship", x = "PID Scale (Dem → Rep)", y = "Pr(Vote Biden)")

# Combine into 2x2 grid
final_plot_2020 <- plot_grid(p1, p2, p3, p4, labels = c("A", "B", "C", "D"), ncol = 2)
final_plot_2020
